package main

import (
	"AppBigDevops/src/common"
	"AppBigDevops/src/config"
	"AppBigDevops/src/web"
	"flag"
	"fmt"
)

func main() {

	// 主配置文件, 命令行参数
	var (
		configFile string
		//httpAddr   string
	)
	flag.StringVar(&configFile, "config_file", "./server.yml", "The config yml")
	//flag.StringVar(&httpAddr, "http_addr", "0.0.0.0:8080", "http地址")
	//flag.Parse()

	sc, err := config.LoadServer(configFile)
	if err != nil {
		panic(err)
	}
	fmt.Printf("主配置文件路径：%v, sc: %v\n", configFile, sc)

	// zap日志
	/**
	// 方式1：快速使用
	logger, _ := zap.NewProduction()
	defer logger.Sync() // flushes buffer, if any
	sugar := logger.Sugar()
	sugar.Infof("Failed to fetch URL: %s", "/login") // Infof  {"msg":"Failed to fetch URL: /login"}
	sugar.Infow(
		"failed to fetch URL",

		// Structured context as loosely typed key-value pairs.
		"url", "/login",
		"attempt", 3,
		"backoff", time.Second,
	)
	// Infow {"msg":"failed to fetch URL","url":"/login","attempt":3,"backoff":1}

	// 方式2：对性能有要求的 使用 Logger
	Logger, _ := zap.NewProduction()
	defer Logger.Sync()
	Logger.Info("failed to fetch URL",
		// Structured context as strongly typed Field values.
		zap.String("url", "/Logger"),
		zap.Int("attempt", 3),
		zap.Duration("backoff", time.Second),
	)
	*/

	// 方式3：自定义logger并配置
	logger := common.NewZapLogger(sc.LogLevel, sc.LogFilePath)
	defer logger.Sync()
	//logger.Info("failed to fetch URL",
	//	// Structured context as strongly typed Field values.
	//	zap.String("url", "/Logger"),
	//	zap.Int("attempt", 3),
	//	zap.Duration("backoff", time.Second),
	//)
	// default字段 + 自定义字段
	// {"level":"info","time":"2024-04-17 00:40:01.728","line":"E:/330 - Golang/allProject/src/AppBigDevops/cmd/server/main.go:71","func":"main.main",
	//"url":"/Logger","attempt":3,"backoff":1

	//logger.Debug(
	//	"测试debug日志",
	//	zap.String("级别", sc.LogLevel),
	//)
	//logger.Info(
	//	"测试Info日志",
	//	zap.String("级别", sc.LogLevel),
	//)
	// 测试日志轮转，切割
	//for i := 0; i < 100000; i++ {
	//	logger.Info("测试当前行数", zap.Int("line", i))
	//}

	// 把logger设置到config中
	sc.Logger = logger

	// 方式1：启动gin
	//r := gin.Default()
	//r.GET("/ping", func(c *gin.Context) {
	//	c.JSON(http.StatusOK, gin.H{
	//		"message": "pong",
	//	})
	//})
	//r.Run(sc.HttpAddr)

	// 方式2：单独启动gin
	err = web.StartGin(sc)
}
